
pub struct Http {
    host: String,
    port: String,
    token: String
}

impl Http {
    pub fn new(host: String, port: String, token: String) -> Self { Self { host, port, token } }
}

impl Http {
    pub async fn get(&self, action: &str) -> anyhow::Result<String> {
        let (http, url, token) = self.args(action);
        Ok(http.get(url).header("Authorization", token).send().await?.text().await?)
    }
    pub async fn post(&self, action: &str, body: String) -> anyhow::Result<String> {
        let (http, url, token) = self.args(action);
        Ok(http.post(url).header("Authorization", token).body(body).send().await?.text().await?)
    }
}

impl Http {
    fn args(&self, action: &str) -> (reqwest::Client, String, String) {
        (
            reqwest::Client::new(),
            format!("http://{}:{}/agilorapi/v6/{}", self.host, self.port, action),
            format!("Token {}", self.token)
        )
    }
}